package com.flower.hooc.service.impl;

import com.flower.hooc.bean.pojo.entity.Course;
import com.flower.hooc.bean.pojo.entity.UserCourse;
import com.flower.hooc.mapper.CourseMapper;
import com.flower.hooc.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:kevinfoo
 * @date:2020/08/29
 * @file:com.flower.service.impl
 */
@Service
public class CourseServiceImpl implements CourseService {

    private Logger logger = LoggerFactory.getLogger(CourseServiceImpl.class);
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> showAll() {
        List<Course> courses = courseMapper.selectAll();
        if (courses.size() == 0) {
            logger.warn("showAll coursesList size is zero [coursesList:{}]", courses);
            throw new RuntimeException("课程数量为0");
        }
        return courses;
    }

    /**
     * 根据id查询课程信息
     *
     * @param id
     * @return
     */
    @Override
    public Course getCourseById(Long id) {
        Course course = courseMapper.getCourseById(id);
        if (course == null) {
            logger.warn("id:{}对应得课程不存在", id);
            throw new RuntimeException("id对应得课程不存在");
        }
        return course;
    }

    /**
     * 跟据用户id查询用户所选课程信息
     *
     * @param id
     * @return
     */
    @Override
    public List<UserCourse> getCourseByUserId(Long id) {
        List<UserCourse> courseByUserId = courseMapper.getCourseByUserId(id);
        if (courseByUserId == null) {
            logger.warn("id:{}用户尚未选择课程", id);
            throw new RuntimeException("用户尚未选择课程");
        }
        return courseByUserId;
    }

    /**
     * 根据课程名称查询课程
     *
     * @param courseName：课程名称
     * @return ：Course
     */
    @Override
    public List<Course> getCourseByName(String courseName) {
        List<Course> courses = courseMapper.getCourseByName(courseName);
        if (courses.size() == 0) {
            logger.warn("showAll coursesList size is zero [coursesList:{}]", courses);
            throw new RuntimeException("查询到的课程数量为0");
        }
        return courses;
    }
}
